package com.avantis.admin.controller;

import com.avantis.admin.config.shiro.CurrentUser;
import com.avantis.admin.service.CompanyService;
import com.avantis.common.base.BaseController;
import com.avantis.common.entity.AdminUser;
import com.avantis.common.entity.Company;
import com.avantis.common.msg.YResult;
import com.avantis.common.utils.DateUtils;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


@RestController
@RequestMapping("/company")
public class CompanyController extends BaseController {

    @Autowired
    protected StringRedisTemplate writeTemplate;

    private static final Logger LOGGER = LoggerFactory.getLogger(CompanyController.class);

    @Autowired
    private CompanyService companyService;

    @PostMapping("/insert")
    @ApiOperation(value = "企业添加接口", httpMethod = "POST", notes = "")
    public YResult insert(@RequestBody @Validated Company company) {
        YResult yResult = restProcessor(() -> {
            boolean cp = companyService.checkByCompanyName(company.getCompanyName());
            if (!cp) {
                AdminUser adminUser = CurrentUser.get();
                company.setCreater(adminUser.getUsername());
                company.setCreatedon(new Date());
                company.setModifyon(new Date());
                companyService.saveCompany(company);
                return  YResult.ok();
            }else {
                return YResult.error("次仓库已经存在,无法添加！");
            }
        });
        return yResult;
    }
}
